package timing.ukulele.third.msgChannel.mq.consumer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import timing.ukulele.third.constant.MessageMqConstants;
import timing.ukulele.third.msgChannel.action.send.SendMessageDirectAction;
import timing.ukulele.third.msgChannel.model.SendTaskInfo;

/**
 * @Description：接收mq消息
 * @Author zh
 * @Date 2025-04-03 17:22
 */
@Component
@Slf4j
public class MessageRabbitMqConsumer {

    private final SendMessageDirectAction sendMessageDirectAction;

    public MessageRabbitMqConsumer(SendMessageDirectAction sendMessageDirectAction) {
        this.sendMessageDirectAction = sendMessageDirectAction;
    }

    /**
     * 消息中心代发消息回调监听
     * @param message
     */
    @RabbitListener(queues = MessageMqConstants.MESSAGE_SEND_DIRECT_QUEUE)
    public void getCallBackMessage(Message message) {
        // 获取消息体
        String messageBodyStr = new String(message.getBody());
        log.info("消息中心代发消息回调监听开始处理：" + messageBodyStr);
        try {
            SendTaskInfo sendTaskInfo = JSON.parseObject(messageBodyStr, SendTaskInfo.class);
            sendMessageDirectAction.process(sendTaskInfo);
            log.info("消息中心代发消息回调监听处理成功");
        }catch (Exception e){
            log.error("消息中心代发消息回调监听处理失败：" + e.getMessage());
        }
    }

}
